import Axios from 'axios'
import request from '@/utils/request'
const cfgBaseUrl = 'https://cfg-xzapi.cqttech.com'
/**
 * 优惠券领取列表
 * http://192.168.88.247:4999/web/#/105/1914
 * @param {*} params
 * @returns
 */
export function getCouponList (params, noMsg = false) {
  return request({
    url: '/api/v1/coupon/list',
    method: 'get',
    noMsg,
    params
  })
}
/**
 * 领取优惠券
 * http://192.168.88.247:4999/web/#/105/1915
 * @param {*} data
 * @returns
 */
export function drawCoupon (data) {
  return request({
    url: '/api/v1/coupon/draw',
    method: 'post',
    data
  })
}
/**
 * 用户领取优惠券的列表
 * http://192.168.88.247:4999/web/#/105/1916
 * @param {*} params
 * @returns
 */
export function getUserCouponList (params) {
  return request({
    url: '/api/v1/coupon/draw_list',
    method: 'get',
    params
  })
}
/**
 * 权益包关联可用的优惠券
 * http://192.168.88.247:4999/web/#/105/1917
 * @param {*} params
 * @returns
 */
export function getUsageCouponList (params) {
  return request({
    url: '/api/v1/coupon/usage_list',
    method: 'get',
    params
  })
}

// 获取场景优惠券的配置
export function getSceneCouponConfig (params) {
  return Axios({
    baseURL: cfgBaseUrl,
    url: `/api/v1/cs/configdata`,
    method: 'get',
    params
  })
}

// 获取优惠券详情
export function getSceneCouponDetail (data) {
  return request({
    url: '/api/v1/coupon/check_can_draw',
    method: 'post',
    data
  })
}
export async function getSceneCouponDetailByIds (ids) {
  const queues = ids.map((v) => getSceneCouponDetail({ coupon_id: Number(v) }))
  const res = await Promise.all(queues)
  let acts = []
  res.forEach((v) => {
    if (['ok', 'draw_valid'].includes(v.data?.sub_code) && v.data?.detail) {
      acts.push(v.data.detail)
    }
  })
  if (acts.length === 0) {
    acts = null
  }
  return acts
}

// 批量领取优惠券
export function useSceneCoupon (data) {
  return request({
    url: '/api/v1/coupon/multi_draw',
    method: 'post',
    data
  })
}
// 领取自动发放的接口
export function autoDrawCoupon (data) {
  return request({
    url: '/api/v1/coupon/auto/draw',
    method: 'post',
    noMsg: true,
    data
  })
}
